<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 组合总和 -->
    <script>
      var combinationSum = function (candidates, target) {
        var res = []
        candidates = candidates.sort()
        function dfs(i, sum, ans) {
          if (sum > target) return
          if (sum == target) {
            res.push([...ans])
          }
          for (i; i < candidates.length; i++) {
            sum += candidates[i]
            ans.push(candidates[i])
            dfs(i + 1, sum, ans)
            sum -= candidates[i]
            ans.pop()
            while (candidates[i + 1] == candidates[i]) i++
          }
        }
        dfs(0, 0, [])
        return res
      }
      console.log(combinationSum([10, 1, 2, 7, 6, 1, 5], 8))
    </script>
  </body>
</html>
